<?php
    namespace app\index\controller;

    use think\Controller;

    class Check extends Controller
    {
        public function checkUser()
        {
            $post = input('post.','','htmlspecialchars'); 
            $is_user = db('user')->where('user',$post['user'])->count();
            $is_email = db('user')->where('email',$post['email'])->count();
            $is_pho = db('user')->where('pho',$post['pho'])->count();
            if($is_user)
            {
                $this->error('用户名已存在');
                die;
            }
            if($is_email)
            {
                $this->error('邮箱已存在');
                die;
            }
            if($is_pho)
            {
                $this->error('号码已存在');
                die;
            }
            $checkBox = validate('Regcheck');
            if(isset($post['up']))
            {
                if(session('random')==$post['phoVail']&&session('pho')==$post['pho'])
                {
                    if($post['pwd']==$post['agPwd'])
                    {
                        if($checkBox->check($post))
                        {
                            session('pho','');
                            unset($post['up'],$post['agPwd'],$post['phoVail']);
                            $post['pwd'] = md5($post['pwd']);
                            switch(cache('user')['logintype'])
                            {
                                case "wechat":
                                    $post['wechat_id'] = cache('user')['openid'];
                                    break;
                                case "weibo":
                                    $post['weibo_id'] = cache('user')['id'];
                                    break;
                                default:
                                    break;
                            }
                            $query = db('user')->insert($post);
                            if($query)
                            {
                                $user = db('user')->where('pho',$post['pho'])->select();
                                session('user',$user);
                                $this->success('注册成功',url('index/index'));
                            }else
                            {
                                $this->success('注册失败',url('user/login'));
                            }
                        }else
                        {
                            $this->error($checkBox->getError());
                        }
                    }else
                    {
                        $this->error('密码不相同');
                    }
                }else
                {
                    $this->error('验证码错误');
                }
            }
        }

        public function phoVail()
        {
            $pho = input('post.pho','');
            if(empty($pho))
            {
                die;
            }
            $random = mt_rand(1000,9999);
            session('random',$random);
            session('pho',$pho);
            $url = 'http://v.juhe.cn/sms/send?mobile='.$pho.'&tpl_id=110957&tpl_value='.urlencode('#code#='.$random).'&key=71cf36d9e8733621c79c9f9bd2ef9b6c';
            curl($url,[]);
            return 1;
        }

        public function weibo()
        {            
            $code = input('get.code');
            $data = [
                'id' => 2891905678,
                'secret' => '93a78135249f8f8010bbd203ec903722',
                'url' => 'http://www.thinkphpp.com/index.php/index/operation/weibo',
                'code' => $code
            ]; 
            $url = 'https://api.weibo.com/oauth2/access_token?client_id='.$data['id'].'&client_secret='.$data['secret'].'&grant_type=authorization_code&redirect_uri='.$data['url'].'&code='.$data['code'];            
            $getUid = curl($url,[]);            
            $phpData = json_decode($getUid,true);            
            $url = "https://api.weibo.com/2/users/show.json?access_token=".$phpData['access_token']."&uid=".$phpData['uid'];            
            $getUser = curl($url);   
            $phpData = json_decode($getUser,true);
            $find_user = db('user')->where('weibo_id',$phpData['id'])->select();
            if($find_user)
            {
                session('user',$find_user);
                $this->success('登录成功',url('index/index'));
            }else
            {
                $phpData['logintype'] = 'weibo';
                cache('user',$phpData,1800);
                $this->redirect(url('user/regthird'),302);
            }
        }

        public function wechat()
        {
            $code = input('get.code');
            $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx02c6c3c8c1a73dac&secret=b7af660aa34a7d85e0dbfda8803d3314&code=".$code."&grant_type=authorization_code";
            $json = curl($url);
            $phpData = json_decode($json,true);
            $url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$phpData['access_token']."&openid=".$phpData['openid']."&lang=zh_CN";
            $userjson = curl($url);
            $user = json_decode($userjson,true);
            $find_user = db('user')->where('wechat_id',$user['openid'])->select();
            if($find_user)
            {
                session('user',$find_user);
                $this->success('登录成功',url('index/index'));
            }else
            {
                $user['logintype'] = 'wechat';
                cache('user',$user,1800);
                $this->redirect(url('user/regthird'),302);
            }
        }
    }
?>